/**
 * 更新至最新的sdk版本1.0.7
 * Class ContextMenu
 */
class ContextMenu {
    constructor() {
        this.rtMenuObj = {
            hasChild: false,
            list: [
                { index: "0", name: "透明度调整", key: "setOpacity", state: false },
                { index: "1", name: "缩放至图层", key: "zoomToLayerById", state: false },
                // { index: "2", name: "图层样式", key: "setLayerStyle", state: false },
                // { index: "3", name: "版本切换", key: "setVersion", state: false },
                { index: "2", name: "图层信息", key: "getLayerInfo", state: false },
                // { index: "5", name: "模型亮度", key: "setModelColouration", state: false },
                // { index: "6", name: "图层属性表", key: "getLayerPropertySheet", state: false },
                // { index: "7", name: "选择空间", key: "selectSpace", state: false },
            ],
        }
    }
    /**
     * @description: 赋值
     * @param {*} parentDom 
     */
    setEnvi(parentDom) {
        this.parentDom = parentDom;
    }
    /**
     * @description: 展示menue
     * @param {*} event 
     * @param {*} data 
     */
    showMenu(event, data) {
        this.resetRMenuListState()
        let _this = this.parentDom;

        _this.curSelectNode = data;
        //在这里根据layerType等任意属性，加不同菜单
        var layerType = _this.curSelectNode.layerType;
        let menuList = [2];
        //影像图层
        if (this.containsSupportType("020202,020302,021101,021102,050002,050003,030100,010100,030002, 030003, 030200, 010200,110200,110100,070100,090100,050100", layerType)) {
            menuList.push(...[0]);
        }
        // //地形图层
        // if (this.containsSupportType("130010, 014000", layerType)) {
        //     this.setRMenuList([2]);
        // }
        //3d tiles
        if (this.containsSupportType("120010,013000", layerType)) {
            // menuList.push(...[0, 2, 5, 6]);
            menuList.push(...[0]);
        }
        //wfs
        if (this.containsSupportType("040003, 030300, 010300,021200,110310", layerType)) {
            //3d点
            // if ("3D" === _this.curSelectNode.featureType) {
            //     menuList.push(...[0, 2, 6]);
            // } else {
            //     menuList.push(...[0, 2, 6]);
            // }
            if ("3D" === _this.curSelectNode.featureType) {
                menuList.push(...[0]);
            } else {
                menuList.push(...[0]);
            }
        }
        // 空间包图层
        if (this.containsSupportType("015000", layerType)) {
            menuList.push(...[0, 7]);
        }
        //i3s
        if (this.containsSupportType("021300,021400", layerType)) {
            menuList.push(...[0]);
        }
        //版本切换
        // if (_this.curSelectNode.proxyType === "2") {
        //     _this.$set(this.rtMenuObj.list[3], "state", true);
        //     _this.verArr = _this.curSelectNode.layerVersion.split(",");
        // } else {
        //     _this.$set(this.rtMenuObj.list[3], "state", false);
        // }

        this.setRMenuList(menuList);

        this.getMenuPosition(event);
    } 
    /**
     * @description: 确定菜单位置
     * @param {*} event 
     */
    getMenuPosition(event) {
        let _this = this.parentDom;

        const hl = agcim.ui.domNode.getDomTopLeft(_this.$refs.mtreeBox);
        _this.isRtMenuShow = true;
        _this.$nextTick(() => {
            let h =
                document.documentElement.clientHeight || document.body.clientHeight;
            let l = event.clientX - hl.left > 240 ? 240 : event.clientX - hl.left;
            let t =
                h - event.clientY - hl.top < 200
                    ? event.clientY - hl.top - _this.$refs.rtMenuBox.offsetHeight
                    : event.clientY - hl.top;
            _this.$refs.rtMenuBox.style.left = l + "px";
            _this.$refs.rtMenuBox.style.top = t + "px";

            if (_this.curSelectNode.children) {
                this.rtMenuObj.hasChild = true;
            } else {
                this.rtMenuObj.hasChild = false;
            }
            //透明度显示位置
            _this.$refs.opacityPoa.style.left = event.clientX - hl.left + "px";
            _this.$refs.opacityPoa.style.top = event.clientY - hl.top + "px";

            //版本切换显示位置
            _this.$refs.versionPoa.style.left = event.clientX - hl.left + "px";
            _this.$refs.versionPoa.style.top = event.clientY - hl.top + "px";

            //版本切换显示位置
            _this.$refs.modelColourationPoa.style.left = event.clientX - hl.left + "px";
            _this.$refs.modelColourationPoa.style.top = event.clientY - hl.top + "px";
        });
    }
   //清空上一次state的设置
    resetRMenuListState(){
        let _this = this.parentDom;
        let arr = this.rtMenuObj.list;
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].state){
                _this.$set(arr[i], "state", false);
            }          
        }
    }
    /**
     * @description: 菜单列表
     * @param {*} list 
     */
    setRMenuList(list) {
        let _this = this.parentDom;

        let arr = this.rtMenuObj.list;
        for (let i = 0; i < arr.length; i++) {
            if (list.indexOf(i) > -1) {
                _this.$set(arr[i], "state", true);
            } else {
                _this.$set(arr[i], "state", false);
            }
        }
    }
    /**
     * @description: 是否包含子字符串
     * @param {*} supportTypes 
     * @param {*} layerType 
     * @returns 
     */
    containsSupportType(supportTypes, layerType) {
        return supportTypes.indexOf(layerType) >= 0;
    }
}


export default new ContextMenu();